/*
 * @Descripttion :
 * @Versions     : 0.1
 * @Author       : foxui team
 * @Date         : 2022-03-01 20:23:31
 * @LastEditors  : QianFox Team
 * @LastEditTime : 2022-06-01 10:07:44
 */

function Textarea() {
    this.TEXTAREA = '.foxui-textarea textarea';
    this.COUNT = '.foxui-textarea-count';
}

Textarea.prototype._init = function () {
    this._textareaInit();
    this._bindCountInput();
};

Textarea.prototype._textareaInit = function () {
    let that = this;
    // 后追加的不能初始化显示 --> 待解决 bug
    $(document).ready(function () {
        $(that.TEXTAREA).each(function () {
            that._setCount(this);
        });
    });
};

Textarea.prototype._bindCountInput = function () {
    let that = this;
    $(document).on('input', this.TEXTAREA, function () {
        that._setCount(this);
    });
};

Textarea.prototype._setCount = function (textarea) {
    let $textarea = $(textarea);
    let len = $textarea.val().length;
    let maxLen = $textarea.attr('maxLength');
    $textarea.siblings(this.COUNT).text(len + '/' + maxLen);
};

let textarea = new Textarea();

export { textarea };
